package com.doctorliang.cheng.shopping.type.fragment;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.doctorliang.cheng.shopping.R;
import com.doctorliang.cheng.shopping.base.BaseFragment;
import com.doctorliang.cheng.shopping.type.adapter.TypeLeftAdapter;
import com.doctorliang.cheng.shopping.type.adapter.TypeRightAdapter;
import com.doctorliang.cheng.shopping.type.bean.TypeBean;
import com.doctorliang.cheng.shopping.utils.Constants;
import com.doctorliang.cheng.shopping.utils.ToastUtil;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Request;

/**
 * 分类页面
 * Created by YBH on 2017/12/14.
 */

public class ListFragment extends BaseFragment {
    @Bind(R.id.lv_left)
    ListView lvLeft;
    @Bind(R.id.rv_right)
    RecyclerView rvRight;

    //分类url数组
    private String[] urls = new String[]{Constants.SKIRT_URL, Constants.JACKET_URL, Constants.PANTS_URL, Constants.OVERCOAT_URL,
            Constants.ACCESSORY_URL, Constants.BAG_URL, Constants.DRESS_UP_URL, Constants.HOME_PRODUCTS_URL, Constants.STATIONERY_URL,
            Constants.DIGIT_URL, Constants.GAME_URL};
    private List<TypeBean.ResultBean> result;
    private boolean isFirst = true;
    private TypeLeftAdapter leftAdapter;
    private TypeRightAdapter typeRightAdapter;


    @Override
    public View initView() {
        View view = View.inflate(mcontext, R.layout.fragment_list, null);
        return view;
    }

    @Override
    public void initData() {
        super.initData();
        //联网请求
        getDataFromNet(urls[0]);
    }

    public void getDataFromNet(String url) {
        Log.e("TAG", "联网" );
//        RequestParams params = new RequestParams(url);
//        x.http().get(params, new Callback.CommonCallback<String>() {
//
//            @Override
//            public void onSuccess(String result) {
//                if (result != null) {
//                    //解析数据
//                    processData(result);
////                    if (isFirst) {
//                    leftAdapter = new TypeLeftAdapter(mcontext);
//                    lvLeft.setAdapter(leftAdapter);
////                    }
//
//                } else {
//                    ToastUtil.show(mcontext,"response = null",Toast.LENGTH_SHORT);
//                }
//            }
//
//            @Override
//            public void onError(Throwable ex, boolean isOnCallback) {
//
//            }
//
//            @Override
//            public void onCancelled(CancelledException cex) {
//
//            }
//
//            @Override
//            public void onFinished() {
//
//            }
//        });
        OkHttpUtils.get().url(url).id(100).build().execute(new MyStringCallBack());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    public class MyStringCallBack extends StringCallback {
        @Override
        public void onBefore(Request request, int id) {
//            super.onBefore(request, id);
        }

        @Override
        public void onAfter(int id) {
//            super.onAfter(id);
        }

        @Override
        public void onError(Call call, Exception e, int id) {
            Log.e("TAG", "联网失败" + e.getMessage());
        }

        @Override
        public void onResponse(String response, int id) {
            switch (id) {
                case 100:
//                    ToastUtil.show(mcontext,"http",Toast.LENGTH_SHORT);
                    if (response != null) {
                        //解析左边数据
                        processData(response);
                        if (isFirst) {
                            leftAdapter = new TypeLeftAdapter(mcontext);
                            lvLeft.setAdapter(leftAdapter);
                        }

                    } else {
                        ToastUtil.show(mcontext,"response = null",Toast.LENGTH_SHORT);
                    }
                    //监听左边
                    initListener(leftAdapter);

                    //解析右边数据（result就是）
                    typeRightAdapter = new TypeRightAdapter(mcontext, result);
                    rvRight.setAdapter(typeRightAdapter);

                    //通过GridLayoutManager实现多样式布局
                    //切记不要忘记设置布局管理者
                    GridLayoutManager manager = new GridLayoutManager(getActivity(), 3);//一个是上下文对象，另一个是一行显示几列的参数常量

                    manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                        @Override
                        public int getSpanSize(int position) {
                            if (position == 0) {
                                return 3;
                            } else {
                                return 1;
                            }
                        }
                    });
                    rvRight.setLayoutManager(manager);

                    break;
                case 101:
                    ToastUtil.show(mcontext,"https",Toast.LENGTH_SHORT);
                    break;

            }
        }
    }

    private void initListener(final TypeLeftAdapter leftAdapter) {
        //点击监听
        lvLeft.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //每点击一下就刷新一下
                leftAdapter.changeSelect(position);
                //默认第0个是true
                if (position!=0) {
                    isFirst = false;
                }
                //再请求网络
                getDataFromNet(urls[position]);
                leftAdapter.notifyDataSetChanged();
            }
        });

        //选中监听
        lvLeft.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                leftAdapter.changeSelect(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    private void processData(String response) {
        TypeBean typeBean = new Gson().fromJson(response, TypeBean.class);
        result = typeBean.getResult();
    }
}
